package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/swap-nodes-in-pairs/description/
 * @create 2025/9/26 15:23
 * @since JDK17
 */

public class Solution64 {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode cur = head.next, pre = head, next = head, virtualNode = new ListNode(-1, head), last = virtualNode;
        while (true) {
            next = cur.next;
            cur.next = pre;
            pre.next = next;
            last.next = cur;
            if (next == null || next.next == null) {
                return virtualNode.next;
            }
            last = pre;
            pre = next;
            cur = pre.next;
        }
    }
}
